#include "LogginDialog.h"

LogginDialog::LogginDialog(User *user,  callback_loggin back)
{
    this->user = user;
    v_user = new QVector<QString>();

    layout_main = new QVBoxLayout;
    layout_account = new QHBoxLayout;
    layout_password = new QHBoxLayout;
    layout_button = new QHBoxLayout;

    label_account = new QLabel();
    label_password = new QLabel();

    input_account = new QLineEdit();
    input_password = new QLineEdit();

    but_loggin = new QPushButton();
    but_regist = new QPushButton();

    call_back = back;

    setFixedSize(240, 160);
}

LogginDialog::~LogginDialog()
{
    delete but_regist;
    delete but_loggin;

    delete input_password;
    delete input_account;

    delete label_password;
    delete label_account;

    delete layout_button;
    delete layout_password;
    delete layout_account;
    delete layout_main;

    delete v_user;
}

void LogginDialog::init_view_main()
{
    setWindowTitle("用户登陆");

    layout_main->setDirection(QBoxLayout::TopToBottom);//设置排列方式
    layout_main->setSpacing(10);    //设置控件距离
    setLayout(layout_main);

    layout_account->setDirection(QBoxLayout::LeftToRight);//设置排列方式
    layout_account->setSpacing(10);    //设置控件距离
    layout_main->addLayout(layout_account);
    init_view_account();

    layout_password->setDirection(QBoxLayout::LeftToRight);//设置排列方式
    layout_password->setSpacing(10);    //设置控件距离
    layout_main->addLayout(layout_password);
    init_view_password();

    layout_button->setDirection(QBoxLayout::LeftToRight);//设置排列方式
    layout_button->setSpacing(10);    //设置控件距离
    layout_main->addLayout(layout_button);
    init_view_button();
}

void LogginDialog::init_view_account()
{
    label_account->setText("账户");
    label_account->setStyleSheet("QLabel{font:15px;padding: 5px 15px 5px 15px;}");
    label_account->setAlignment(Qt::AlignCenter);
    layout_account->addWidget(label_account, 1);

    input_account->setPlaceholderText("请输入账户");
    input_account->setStyleSheet("QLabel{font:15px;padding: 0px 5px 0px 5px;}");
    input_account->setClearButtonEnabled(true);
    layout_account->addWidget(input_account, 10);
}

void LogginDialog::init_view_password()
{
    label_password->setText("密码");
    label_password->setStyleSheet("QLabel{font:15px;padding: 5px 15px 5px 15px;}");
    label_password->setAlignment(Qt::AlignCenter);
    layout_password->addWidget(label_password, 1);

    input_password->setPlaceholderText("请输入密码");
    input_password->setStyleSheet("QLabel{font:15px;padding: 0px 5px 0px 5px;}");
    input_password->setClearButtonEnabled(true);
    input_password->setEchoMode(QLineEdit::Password);
    layout_password->addWidget(input_password, 10);
}

void LogginDialog::init_view_button()
{
    but_loggin->setText("登陆");
    QObject::connect(but_loggin,&QPushButton::clicked,but_loggin,[&](){
        QString account = input_account->text().isEmpty() ? NULL : input_account->text();
        QString password = input_password->text().isEmpty() ? NULL : input_password->text();
        if(loggin(account,password))
        {
            std::cout<< "loggin OK!" << std::endl;
            user->set_user(v_user);
            setWindowTitle("登陆成功");
            (*call_back)();
        }
        else
        {
            std::cout<< "loggin failed!" << std::endl;
            setWindowTitle("登陆失败");
        }
    }); //设置监听
    layout_button->addWidget(but_loggin,1);

    but_regist->setText("注册");
    QObject::connect(but_regist,&QPushButton::clicked,but_regist,[&](){
        QString account = input_account->text().isEmpty() ? NULL : input_account->text();
        QString password = input_password->text().isEmpty() ? NULL : input_password->text();
        if(regist(account,password))
        {
            std::cout<< "regist OK!" << std::endl;
            user->set_user(v_user);
            setWindowTitle("注册成功");
            (*call_back)();
        }
        else
        {
            std::cout<< "regist failed!" << std::endl;
            setWindowTitle("注册失败");
        }
    }); //设置监听
    layout_button->addWidget(but_regist,1);
}

int LogginDialog::loggin(QString account, QString password)
{
    if(account.isNull() || password.isNull())
    {
        return 0;
    }

    std::cout << account.toStdString() << std::endl;
    std::cout << password.toStdString() << std::endl;

    SQLHelper sql_helper;
    sql_helper.connect();
    QVector<QVector<QString>*> result;

    QString condition = "name='" + account +"' AND passwd='" + password + "'";
    if(sql_helper.search_table("user",NULL,condition, &result))
    {
        if(result.count() > 0)
        {
            for(int j = 0; j < result[0]->size(); j++)
            {
                v_user->push_back((*result[0])[j]);   // id
            }
        }
        return result.size();
    }
    return 0;
}


int LogginDialog::regist(QString account, QString password)
{
    if(account.isNull() || password.isNull())
    {
        return 0;
    }

    std::cout << account.toStdString() << std::endl;
    std::cout << password.toStdString() << std::endl;

    SQLHelper sql_helper;
    sql_helper.connect();
    QVector<QVector<QString>*> result;

    QString condition = "name='" + account +"'";
    if(sql_helper.search_table("user",NULL,condition, &result))
    {
        if(result.size())
        {
            return 0;
        }
    }

    time_t now = time(0);
    v_user->push_back(QString::number(now % 1000000)); // id
    v_user->push_back(account);    // name
    v_user->push_back(password);    // passwd
    v_user->push_back(QDateTime::currentDateTime().toString("yyyy-MM-dd:hh:mm:ss"));   // create_time
    v_user->push_back("1");   // authority

    return sql_helper.insert_table("user", v_user);
}

